package eu.irreality.dai.gameplay;

import java.awt.KeyboardFocusManager;

import eu.irreality.dai.fov.UniversalLOS;
import eu.irreality.dai.gameplay.actions.movement.StandardPassabilityCriterion;
import eu.irreality.dai.gameplay.entities.PlayerCharacter;
import eu.irreality.dai.gameplay.entities.factory.EntityFactory;
import eu.irreality.dai.gameplay.world.GameWorld;
import eu.irreality.dai.ui.keyboard.DaiKeyListener;
import eu.irreality.dai.ui.keyboard.EventDispatcher;
import eu.irreality.dai.ui.keyboard.KeyProcessor;
import eu.irreality.dai.ui.keyboard.KeyReceiver;
import eu.irreality.dai.ui.keyboard.RealKeyReceiver;
import eu.irreality.dai.ui.simascii.SimulatedAsciiDisplay;
import eu.irreality.dai.ui.wrapper.Display;
import eu.irreality.dai.ui.wrapper.DisplayFrame;
import eu.irreality.dai.util.Roll;
import eu.irreality.dai.world.gen.LevelGenerator;
import eu.irreality.dai.world.level.Level;

public class TestGameplay
{

    public static void main(String[] args)
    {

	// Debug.set(true);
	// LevelGenerator dg = new DungeonGenerator();
	// LevelGenerator cacg = new CACavernousLevelGenerator();
	// Level l = dg.generate(25, 80);
	// l = cacg.generate(25, 80);

	GameWorld gw = new GameWorld();

	gw.generateTestDungeon();

	Level l = gw.getLevel("TD", 0);

	PlayerCharacter pc = EntityFactory.createPlayerCharacter();
	pc.setPassabilityCriterion(new StandardPassabilityCriterion());
	pc.setProperty("monster_action", "idle", 10);
	gw.setPlayerCharacter(pc);

	int row = -1;
	int col = -1;
	do
	{
	    row = Roll.randomInt(l.getRows() - 1);
	    col = Roll.randomInt(l.getCols() - 1);
	} while (l.get(row, col).get(0).equals(LevelGenerator.WALL));
	l.addEntity(pc, row, col);
	pc.setPosition(row, col);

	Display display = new SimulatedAsciiDisplay();
	display.setSize(25, 80);
	display.setLevel(l);

	DisplayFrame test = new DisplayFrame(display);
	gw.addObserver(test);

	gw.setActiveLevel(l);

	KeyProcessor keyProc = new KeyProcessor(gw);

	KeyReceiver receiver = new RealKeyReceiver(keyProc);
	DaiKeyListener listener = new DaiKeyListener(receiver);
	pc.addObserver((RealKeyReceiver) receiver);

	pc.setLOSCriterion(new UniversalLOS());

	// TODO Uncomment this line to see a lousy LOS implementation in action
	// pc.setLOSCriterion(new SimpleLOS(
	// new SimpleVisibilityBlockingCriterion()));

	pc.updateSeen(l);

	test.addKeyListener(listener);
	KeyboardFocusManager manager = KeyboardFocusManager
		.getCurrentKeyboardFocusManager();
	manager.addKeyEventDispatcher(new EventDispatcher(test, manager));

	gw.getGameWorldThread().start();

    }
}
